<?php
/*************************************************************************

$Id: iclear.php 499 2010-10-06 20:33:34Z dis $

iclear payment system - because secure is simply secure
http://www.iclear.de

Copyright (c) 2001 - 2010 iclear - Christian Arnold

Released under the GNU General Public License

************************************************************************

All rights reserved.

This program is free software licensed under the GNU General Public License (GPL).

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA

*************************************************************************/

class IclearWrapperOxid extends IclearWrapperBase {

	var $lang;
	// iclear DMI -> added global order var
	var $code;

	var $title;

	var $description;

	var $enabled;

	var $order;

	var $orderStatusID;

	var $currentProductID = 0;

	var $specialItemID = 0;

	var $showPriceTax;

	var $otPaymentItemID = 0;

	/**
	 *
	 * @var  Local_Iclear_Sendorderb2c_Model_Iclearpayment
	 * @access protected
	 */
	protected $_payment = null;

	/**
	 *
	 * @var  Mage_Sales_Model_Quote
	 * @access protected
	 */
	protected $_order = null;

	protected $_basket = null;

	protected $_user = null;

	protected $_aSession = null;

	var $addressKeysMask = array(
      'name' => '',
      'company' => '',
      'street' => 'info',
      'region' => '',
      'city' => '',
      'postcode' => '',
      'state' => '',
	);

	var $addressKeysUnmask = array(
      'company',
      'street',
      'region',
      'city',
      'postcode',
      'state',
	);

	protected $_shopID = 0;
	
	protected $bIframe = false;

	// class constructor
	function __construct(&$icCore) {
		$this->icVersion = '$Id$';
		global $icCore;
		parent::IclearWrapperBase($icCore);
		if(!defined('IC_WRAPPER_ID')) {
			die("Wrapper base class 4 oxid not instantiated!\n");
		}

		$this->id = IC_WRAPPER_ID;

		$this->code = 'iclear';
		$lang = $icCore->getLanguage();

		$this->lang =& $lang;
		$this->_installType = 'create';
	}

	/**
	 * Performs a SQL query
	 * @param string $sql
	 */
	function dbQuery( $sql = '' ) {
		$oDB = oxDb::getDb( true );
		$rs = $oDB->Execute( $sql );
		return $rs;
	}

	/**
	 * Fetch a single record as array from DB
	 * @param string $sql
	 */
	function dbFetchRecord( $sql = '' ) {
		$rec = false;
		if( $rs = $this->dbQuery( $sql ) ) {
			if ( $rs != false && $rs->RecordCount( ) > 0 ) {
				$rec = $rs->fields;
			}
		}
		return $rec;
	}

	/**
	 * Last DB insert ID
	 */
	function dbLastInsertID() {
		$oDB = oxDb::getDb( true );
		return $oDB->Insert_ID( );
	}

	/**
	 * Entry point of iclear order processing
	 */
	function process( ) {
		if($this->_order) {
			$soap = $this->icCore->getObject('IclearSOAP');
			return $this->icCore->perform($soap, IC_SOAP_SENDORDER_METHOD);
		}
	}

	function setShopID ( $shopID ) {
		$this->_shopID = $shopID;
	}

	function setOrder ( $oOrder ) {
		$this->_order =& $oOrder;
	}

	function setPayment ( $oPayment ) {
		$this->_payment =& $oPayment;
	}

	function setBasket ( $oBasket ) {
		$this->_basket =& $oBasket;
	}

	function setUser ( $oUser ) {
		$this->_user =& $oUser;
	}

	/**
	 * current order object
	 * @return Mage_Sales_Model_Quote
	 */
	function getOrder() {
		return $this->_order;
	}

	/**
	 * actual currency ISO3 code, EUR, e.g.
	 * @return string $currency
	 */
	function currency() {
		$currency = null;
		$currency = $this->_basket->getBasketCurrency()->name;
		if ( $currency ) {
			return $currency;
		}
		return '';
	}
	/**
	 * item count of current order
	 * @return int $count
	 */
	function orderItemCount() {
		$rv = 0;
		if ( $rv = $this->_basket->getProductsCount() > 0 )
		return $rv;
		return 0;
	}

	/**
	 * next item of current order
	 * @return array icSoapItem
	 */
	function nextOrderItem() {
		$item = false;
		$id =& $this->currentProductID;
		$articles =& $this->_basket->getBasketArticles( );
		$articleItems =& $this->_basket->getContents();
		$aKeys = array_keys ( $articles );

		if( is_array( $articles ) ) {
			if( $id < sizeof( $articles ) ) {
				$actArticle =& $articles[$aKeys[$id]];
				$actArticleItems =& $articleItems[$aKeys[$id]];
				$amount = 0.0;
				if ( $actArticleItems->getdBundledAmount( ) > 0 && ( $actArticleItems->isBundle( ) || $actArticleItems->isDiscountArticle( ) ) )
				$amount= $actArticleItems->getdBundledAmount( );
				else
				$amount = $actArticleItems->getAmount( );


				$item[IC_SOAP_ITEM_NO] = $actArticle->getProductId( );
				$item[IC_SOAP_ITEM_TITLE] = $amount . ' x ' . $actArticle->oxarticles__oxtitle->value;
				if ( $actArticleItems->isBundle( ) ) {
					foreach ( $actArticleItems->getChosenSelList() as $key => $value ) {
						$item[IC_SOAP_ITEM_TITLE] .= '<br />';
						$item[IC_SOAP_ITEM_TITLE] .= $key . ': ' . $value;
					}
				}
				$item[IC_SOAP_ITEM_TITLE] = $this->encodeUTF8 ( $item[IC_SOAP_ITEM_TITLE] );
				$item[IC_SOAP_ITEM_QTY] = 1; // bundling items to single article @ iclear
				$item[IC_SOAP_ITEM_PRICE_NET] = $actArticle->getPrice()->getNettoPrice() * $amount;
				$item[IC_SOAP_ITEM_PRICE_GROS] =  $actArticle->getPrice()->getBruttoPrice() * $amount;
				$item[IC_SOAP_ITEM_VAT_RATE] = $actArticleItems->getVatPercent();
			}
			$id++;
		}
		return $item;
	}

	/**
	 * get all specials items from shop (shipping, coupons) as an array
	 * of BasketItem formatted arrays
	 * @return array
	 */
	function specialItems() {
		$oLang = oxLang::getInstance( );
		$items = false;
		
		try {
			if ( method_exists ( $this->_basket, 'getDeliveryCosts' ) ) {
				if ( $this->_basket->getDeliveryCosts( ) ) {
					$item[IC_SOAP_ITEM_NO] = $this->specialItemID;
					$item[IC_SOAP_ITEM_TITLE] = $this->encodeUTF8( $oLang->translateString( 'ORDER_SHIPPINGGROSS1', $oLang->getBaseLanguage( ) ) );
					$item[IC_SOAP_ITEM_PRICE_NET] = $this->_basket->getDeliveryCosts();
					$item[IC_SOAP_ITEM_PRICE_GROS] =  $this->_basket->getDeliveryCosts();
					$item[IC_SOAP_ITEM_QTY] = 1;
					if ( $this->_basket->getDelCostVat( ) )
					$item[IC_SOAP_ITEM_VAT_RATE] = $this->_basket->getDelCostVatPercent();
					else
					$item[IC_SOAP_ITEM_VAT_RATE] = 0.0;
					$items[] = $item;
				}
			}
	
			if ( method_exists ( $this->_basket, 'getPaymentCosts' ) ) {
				if ( $this->_basket->getPaymentCosts( ) ) {
					$item[IC_SOAP_ITEM_NO] = $this->specialItemID;
					$item[IC_SOAP_ITEM_TITLE] = $this->encodeUTF8( $oLang->translateString( 'ORDER_PAYMENT2', $oLang->getBaseLanguage( ) ) );
					$item[IC_SOAP_ITEM_PRICE_NET] = $this->_basket->getPaymentCosts();
					$item[IC_SOAP_ITEM_PRICE_GROS] =  $this->_basket->getPaymentCosts();
					$item[IC_SOAP_ITEM_QTY] = 1;
					if ( $this->_basket->getPayCostVat( ) )
					$item[IC_SOAP_ITEM_VAT_RATE] = $this->_basket->getPayCostVatPercent();
					else
					$item[IC_SOAP_ITEM_VAT_RATE] = 0.0;
					$items[] = $item;
				}
			}
			
			if ( method_exists ( $this->_basket, 'getTsProtectionCosts' ) ) {
				if ( $this->_basket->getTsProtectionCosts( ) ) {
					$item[IC_SOAP_ITEM_NO] = $this->specialItemID;
					$item[IC_SOAP_ITEM_TITLE] = $this->encodeUTF8( $oLang->translateString( 'ORDER_TSPROTECTION', $oLang->getBaseLanguage( ) ) );
					$item[IC_SOAP_ITEM_PRICE_NET] = $this->_basket->getTsProtectionCosts();
					$item[IC_SOAP_ITEM_PRICE_GROS] =  $this->_basket->getTsProtectionCosts();
					$item[IC_SOAP_ITEM_QTY] = 1;
					if ( $this->_basket->getPayCostVat( ) )
					$item[IC_SOAP_ITEM_VAT_RATE] = $this->_basket->getTsProtectionVatPercent();
					else
					$item[IC_SOAP_ITEM_VAT_RATE] = 0.0;
					$items[] = $item;
				}
			}
	
			if ( method_exists ( $this->_basket, 'getWrappCostNet' ) ) {
				if ( $this->_basket->getWrappCostNet( ) ) {
					$item[IC_SOAP_ITEM_NO] = $this->specialItemID;
					$item[IC_SOAP_ITEM_TITLE] = $this->encodeUTF8( $oLang->translateString( 'ORDER_WRAPPINGGROSS1', $oLang->getBaseLanguage( ) ) );
					$item[IC_SOAP_ITEM_PRICE_NET] = $this->_basket->getFWrappingCosts();
					$item[IC_SOAP_ITEM_PRICE_GROS] =  $this->_basket->getFWrappingCosts();
					$item[IC_SOAP_ITEM_QTY] = 1;
					if ( $this->_basket->getPayCostVat( ) )
					$item[IC_SOAP_ITEM_VAT_RATE] = $this->_basket->getWrappCostVatPercent();
					else
					$item[IC_SOAP_ITEM_VAT_RATE] = 0.0;
					$items[] = $item;
				}
			}
			
			if ( method_exists ( $this->_basket, 'getVoucherDiscValue' ) ) {
				if ( $this->_basket->getVoucherDiscValue( ) ) {
						foreach ( $this->_basket->getVouchers( ) as $key => $value ) {
							$item[IC_SOAP_ITEM_NO] = $this->specialItemID;
							$item[IC_SOAP_ITEM_TITLE] = $this->encodeUTF8( $oLang->translateString( 'ORDER_COUPON', $oLang->getBaseLanguage( ) ) . "<br />" . $oLang->translateString( 'ORDER_NOMBER', $oLang->getBaseLanguage( ) ) . " ". $value->sVoucherNr . ")");
							$item[IC_SOAP_ITEM_PRICE_NET] = "-" . $value->dVoucherdiscount;
							$item[IC_SOAP_ITEM_PRICE_GROS] =  "-". $value->dVoucherdiscount;
							$item[IC_SOAP_ITEM_QTY] = 1;
							$item[IC_SOAP_ITEM_VAT_RATE] = 0.0;
							$items[] = $item;
						}
				}
			}
			
			if ( method_exists ( $this->_basket, 'getDiscounts' ) ) {
				if ( $this->_basket->getDiscounts( ) ) {
					foreach ( $this->_basket->getDiscounts( ) as $key => $value ) {
						$item[IC_SOAP_ITEM_NO] = $this->specialItemID;
						if ( $value->dDiscount < 0)
							$item[IC_SOAP_ITEM_TITLE] = $this->encodeUTF8( $oLang->translateString( 'ORDER_CHARGE', $oLang->getBaseLanguage( ) ) );
						else
							$item[IC_SOAP_ITEM_TITLE] = $this->encodeUTF8( $oLang->translateString( 'ORDER_DISCOUNT', $oLang->getBaseLanguage( ) ) );
						$item[IC_SOAP_ITEM_PRICE_NET] = "-" . $value->dDiscount;
						$item[IC_SOAP_ITEM_PRICE_GROS] = "-" . $value->dDiscount;
						$item[IC_SOAP_ITEM_QTY] = 1;
						$item[IC_SOAP_ITEM_VAT_RATE] = 0.0;
						$items[] = $item;
					}
				}
			}
		}
		catch ( exception $e ) {
			die($e->getMessage());
		}
		return $items;
	}

	/**
	 * Get the current delivery address info from shop-system
	 * @return IclearAddress
	 */
	function &deliveryAddress( ) {
		$rv = false;
		$oCountry = oxNew( 'oxCountry' );

		if( $this->getOrder( ) ) {
			if ( $sAddressId = $this->_user->getSelectedAddressId() ) {
				$sSelectedAddress = $this->_user->getSelectedAddress();
				$oAddresses = $this->_user->getUserAddresses();
	
				$oCountry->load ( $oAddresses[$sSelectedAddress]->getFieldData('oxcountryid') );
	
				$params = array(
				IC_SOAP_ADDRESS_FIRSTNAME => $this->encodeUTF8($oAddresses[$sSelectedAddress]->getFieldData('oxfname')),
				IC_SOAP_ADDRESS_LASTNAME => $this->encodeUTF8($oAddresses[$sSelectedAddress]->getFieldData('oxlname')),
				IC_SOAP_ADDRESS_COMPANY => $this->encodeUTF8($oAddresses[$sSelectedAddress]->getFieldData('oxcompany')),
				IC_SOAP_ADDRESS_STREET => $this->encodeUTF8($oAddresses[$sSelectedAddress]->getFieldData('oxstreet')),
				IC_SOAP_ADDRESS_STREET_NO => $this->encodeUTF8($oAddresses[$sSelectedAddress]->getFieldData('oxstreetnr')),
				IC_SOAP_ADDRESS_ZIPCODE => $this->encodeUTF8($oAddresses[$sSelectedAddress]->getFieldData('oxzip')),
				IC_SOAP_ADDRESS_CITY => $this->encodeUTF8($oAddresses[$sSelectedAddress]->getFieldData('oxcity')),
				IC_SOAP_ADDRESS_COUNTRY => $this->encodeUTF8($oCountry->getFieldData('oxisoalpha2')),
				);
				if ( $oAddresses[$sSelectedAddress]->getFieldData('oxsal') == "MR" )
				$params[IC_SOAP_ADDRESS_SALUTATION] = 1;
				else if ( $oAddresses[$sSelectedAddress]->getFieldData('oxsal') == "MRS" )
				$params[IC_SOAP_ADDRESS_SALUTATION] = 0;
				else
				$params[IC_SOAP_ADDRESS_SALUTATION] = -1;
			}
			else {
				$oCountry->load ( $this->_user->getFieldData('oxcountryid') );
				$params = array(
				IC_SOAP_ADDRESS_SALUTATION => $this->encodeUTF8($this->_user->getFieldData('oxsal')),
				IC_SOAP_ADDRESS_FIRSTNAME => $this->encodeUTF8($this->_user->getFieldData('oxfname')),
				IC_SOAP_ADDRESS_LASTNAME => $this->encodeUTF8($this->_user->getFieldData('oxlname')),
				IC_SOAP_ADDRESS_COMPANY => $this->encodeUTF8($this->_user->getFieldData('oxcompany')),
				IC_SOAP_ADDRESS_STREET => $this->encodeUTF8($this->_user->getFieldData('oxstreet')),
				IC_SOAP_ADDRESS_STREET_NO => $this->encodeUTF8($this->_user->getFieldData('oxstreetnr')),
				IC_SOAP_ADDRESS_ZIPCODE => $this->encodeUTF8($this->_user->getFieldData('oxzip')),
				IC_SOAP_ADDRESS_CITY => $this->encodeUTF8($this->_user->getFieldData('oxcity')),
				IC_SOAP_ADDRESS_COUNTRY => $this->encodeUTF8($oCountry->getFieldData('oxisoalpha2')),
				);
			}

			$this->customerInfo = $info;
			$address = $this->icCore->getObject('IclearAddress', $instance = true);
			if($address->address($params)) {
				$rv =& $address;
			}
		}
		return $rv;
	}

	/**
	 * get the customer register information from shop-system
	 * as an array, formatted 4 direct usage in soap call
	 * @return array
	 */
	function customerInfo() {
		$rv = false;
		if(!$this->customerInfo) {
			if( $this->getOrder( ) ) {
				$oCountry = oxNew( 'oxCountry' );
				$oCountry->load ( $this->_user->getFieldData('oxcountryid') );
				
				$info = array(
				IC_SOAP_REG_CUSTOMER_ACQUISE_ID => 0,
				IC_SOAP_REG_CUSTOMER_BANK_ACCOUNT_ID => '',
				IC_SOAP_REG_CUSTOMER_BANK_BIC => '',
				IC_SOAP_REG_CUSTOMER_BANK_BLZ => '',
				IC_SOAP_REG_CUSTOMER_BANK_CUSTOMER => '',
				IC_SOAP_REG_CUSTOMER_BANK_IBAN => '',
				IC_SOAP_REG_CUSTOMER_BANK_NAME => '',
				IC_SOAP_REG_CUSTOMER_COMPANYNAME => $this->encodeUTF8($this->_user->getFieldData('oxcompany')),
				IC_SOAP_REG_CUSTOMER_EMAIL => $this->encodeUTF8($this->_user->getFieldData('oxusername')),
				IC_SOAP_REG_CUSTOMER_FAX => $this->encodeUTF8($this->_user->getFieldData('oxfax')),
				IC_SOAP_REG_CUSTOMER_FIRSTNAME => $this->encodeUTF8($this->_user->getFieldData('oxfname')),
				IC_SOAP_REG_CUSTOMER_DOB => $this->encodeUTF8($this->_user->getFieldData('oxbirthdate') . 'T00:00:00'),
				IC_SOAP_REG_CUSTOMER_INVOICE_CITY => $this->encodeUTF8($this->_user->getFieldData('oxcity')),
				IC_SOAP_REG_CUSTOMER_INVOICE_COUNTRY => $this->encodeUTF8($oCountry->getFieldData('oxisoalpha2')),
				IC_SOAP_REG_CUSTOMER_INVOICE_RECEIVER => $this->encodeUTF8($this->_user->getFieldData('oxfname') . ' ' . $this->_user->getFieldData('oxlname')),
				IC_SOAP_REG_CUSTOMER_INVOICE_STREET => $this->encodeUTF8($this->_user->getFieldData('oxstreet')),
				IC_SOAP_REG_CUSTOMER_INVOICE_STREETNO => $this->encodeUTF8($this->_user->getFieldData('oxstreetnr')),
				IC_SOAP_REG_CUSTOMER_INVOICE_ZIPCODE => $this->encodeUTF8($this->_user->getFieldData('oxzip')),
				IC_SOAP_REG_CUSTOMER_LASTNAME => $this->encodeUTF8($this->_user->getFieldData('oxlname')),
				IC_SOAP_REG_CUSTOMER_PHONE => $this->encodeUTF8($this->_user->getFieldData('oxfax')),
				IC_SOAP_REG_CUSTOMER_EXT_CUSTOMER_ID => $this->encodeUTF8($this->_user->getFieldData('oxcustnr')),
				IC_SOAP_REG_CUSTOMER_NEWSLETTER_FLAG => '',
				);
				if ( $this->_user->getFieldData('oxsal') == "MR" )
					$info[IC_SOAP_REG_CUSTOMER_SALUTATION_ID] = 1;
				else if ( $this->_user->getFieldData('oxsal') == "MRS" )
					$info[IC_SOAP_REG_CUSTOMER_SALUTATION_ID] = 0;
				else
					$info[IC_SOAP_REG_CUSTOMER_SALUTATION_ID] = -1;
				
				$this->customerInfo = $info;
			}

		}

		return $this->customerInfo;
	}

	/**
	 * get or set session ID
	 * @param string $sessionID
	 * @return string $sessionID
	 */
	function sessionID($sessionID = '') {
		return $this->_order->getSession()->getId();
	}

	/**
	 * customerID of current order
	 * @return int
	 */
	function customerID() {
		$rv = 0;
		if($this->_user) {
			$rv = $this->_user->getId(); //TODO OXCUSTNR
		}
		return $rv;
	}

	/**
	 * get the iclear shopID from config
	 * @return int
	 */
	function shopID() {
		return $this->_shopID;
	}

	/**
	 * language ISO2 code
	 * @return string
	 */
	function languageISO() {
		return $this->_order->getActiveLangAbbr();
	}

	/**
	 * returns the iclear URL to the iclear login page
	 * @return string
	 */
	function iclearURL() {
		$rv = '';
		$basket = $this->icCore->getBasket();
		if($url = $basket->iclearURL()) {
			$rv = $basket->iclearURL();
		}
		return $rv;
	}

	/**
	 * obsolete in magento
	 */
	function finalizeBasket() {
	}

	/**
	 * Abstract method which must be implemented in child classes
	 */
	function storeOrder() {
		die("Store order not implemented in enclosing instance!\n");
	}

	/**
	 * recreates the delivery and billing information of a order
	 */
	function unmaskOrder() {
		$rc = false;
		$basket = $this->icCore->getBasket();
		if($orderID = $basket->orderID()) {
			$oOrder = oxNew( 'oxorder' );
			$oOrder->load ( $orderID );
			if ( $oOrder->getId( ) ) {
				$oOrder->iclearUnmaskOrder ( );
				$rc = true;
			}
		}
		return $rc;
	}

	/**
	 * set the order state 2 cancel
	 * works only with waiting orders
	 * @param int $orderID
	 */
	function cancelOrder($orderID = 0) {
		$rc = false;
		if($orderID) {
			$oOrder = oxNew( 'oxorder' );
			$oOrder->load ( $orderID );
			if ( $oOrder->getId( ) ) {
				$oOrder->cancelOrder( );
				$rc = true;
			}
		}
		return $rc;
	}

	/**
	 * returns the shopURL where the iclear system redirects the customer finally to
	 * called from iclear.php processing endpoint
	 */
	function shopURL() {
		$rv = '';
		$basket =& $this->icCore->getBasket();

		if($basket->orderAccepted()) {
			if($basket->status() == IC_ORDER_CANCEL) {
				if ( oxConfig::getInstance( )->getConfigParam( 'sIC_iframe' ) ) {
					$rv = oxConfig::getInstance()->getConfigParam('sShopURL')."index.php?cl=order&fnc=iclear&ic_cancel=1";
				} else {
					$rv = oxConfig::getInstance()->getConfigParam('sShopURL')."index.php?cl=payment";
				}
			} else {
				if ( oxConfig::getInstance( )->getConfigParam( 'sIC_iframe' ) ) {
					$rv = oxConfig::getInstance()->getConfigParam('sShopURL')."index.php?cl=order&fnc=iclear&ic_ok=1";
				} else {
					$rv = oxConfig::getInstance()->getConfigParam('sShopURL')."index.php?cl=thankyou";
				}
			}
		} else {
			$lang =& $this->icCore->getLanguage();
			$soap =& $this->icCore->getObject('IclearSOAP');
			if ( oxConfig::getInstance( )->getConfigParam( 'sIC_iframe' ) ) {
				$rv = oxConfig::getInstance()->getConfigParam('sShopURL')."index.php?cl=order&fnc=iclear&ic_error=1&payerror=-1&payerrortext=" . urlencode( $lang->getParam( 'TRANSACTION_ERROR' ) ) . "&iclearerror=" . urlencode( $soap->lastError( ) );
			} else {
				$rv = oxConfig::getInstance()->getConfigParam('sShopURL')."index.php?cl=payment&payerror=-1&payerrortext=" . urlencode( $lang->getParam( 'TRANSACTION_ERROR' ) ) . "&iclearerror=" . urlencode( $soap->lastError( ) );
			}
		}
		return $rv;
	}

	/**
	 * Installs iclear table in DB, if not present
	 */
	function setup( $clean = true ) {
		$rv = false;
		if (
			$this->installTable( $clean ) &&
			$this->installPayment( ) &&
			$this->installPaymentGroups ( ) && 
			$this->installPaymentDelivery ( ) && 
			$this->installModules ( )
		)
			$rv = true;
		return $rv;
	}

	function beforeSessionSave ( ) {
		if ( !is_null( $_SESSION ) ) {
			$this->_aSession = $_SESSION;
			$_SESSION['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
			$_SESSION['sid_key'] = $_COOKIE['sid_key'];
		}
	}

	function afterSessionSave ( ) {
		if ( !is_null( $this->_aSession ) ) {
			$_SESSION = $this->_aSession;
		}
	}

	function installPayment ( ) {
		$rv = false;
		$oPayment = oxNew ( "oxpayment" );
		$oPayment->load ( IC_OXID_MODULE_NAME );
		if ( $oPayment->getId() != IC_OXID_MODULE_NAME )
		$oPayment->setId ( IC_OXID_MODULE_NAME );
		$oPayment->oxpayments__oxactive = new oxField( "1", oxField::T_RAW );
		$oPayment->oxpayments__oxdesc = new oxField( "iclear Treuhand System", oxField::T_RAW );
		$oPayment->oxpayments__oxaddsumtype = new oxField( "abs", oxField::T_RAW );
		$oPayment->oxpayments__oxtoamount = new oxField( "1000000", oxField::T_RAW );
		$oPayment->oxpayments__oxlongdesc = new oxField( "iclear - einfach, sicher", oxField::T_RAW );
		if ( $oPayment->save() == IC_OXID_MODULE_NAME )
		$rv = true;

		return rv;
	}

	function installPaymentGroups ( ) {
		$rv = false;
		$aAddGroups = array( 'oxidadmin', 'oxiddealer', 'oxidcustomer', 'oxidnewcustomer' );
		foreach ($aAddGroups as $sAddgroup) {
			$oNewGroup = oxNew( "oxobject2group" );
			$oNewGroup->oxobject2group__oxobjectid = new oxField( oxConfig::getInstance()->getShopId(), oxField::T_RAW  );
			$oNewGroup->oxobject2group__oxgroupsid = new oxField( $sAddgroup, oxField::T_RAW );
			$oNewGroup->oxobject2group__oxobjectid = new oxField( IC_OXID_MODULE_NAME, oxField::T_RAW );
			if ( $oNewGroup->save( ) )
				$rv = true;
		}
		return $rv;
	}

	function installPaymentDelivery ( ) {
		$rv = false;
		$oObject = oxNew( 'oxbase' );
		$oObject->init( 'oxobject2payment' );
		$oObject->oxobject2payment__oxpaymentid = new oxField( IC_OXID_MODULE_NAME, oxField::T_RAW );
		$oObject->oxobject2payment__oxobjectid  = new oxField( "oxidstandard", oxField::T_RAW );
		$oObject->oxobject2payment__oxtype      = new oxField( "oxdelset", oxField::T_RAW );
		if ( $oObject->save( ) )
			$rv = true;
		return $rv;
	}
	
	function installModules ( ) {
		$rv = false;
		$aModules = array( );
		$aModulesIclear = array (
				'order' => 'ic_iclear/ic_order',
				'oxorder' => 'ic_iclear/ic_oxorder',
				'payment' => 'ic_iclear/ic_payment',
		);
		
		
		$aModules = oxConfig::getInstance()->getConfigParam( 'aModules' );
		if ( !count ( $aModules ) ) {
			$aModules = $aModulesIclear;
		}
		else {
			foreach ( $aModulesIclear as $key => $value ) {
				if ( isset ($aModules[$key]) ) {
					if ( !strstr( $aModules[$key], $value ) ) {
						$aModules[$key] .= "&" . $value;
					}
				}
				else {
					$aModules[$key] = $value;
				}
			}
		}
		oxConfig::getInstance( )->saveShopConfVar( "aarr", "aModules", $aModules, oxConfig::getInstance()->getShopId() );
		$rv = true;
		return $rv;
	}
	
	function setInline ( $inline ) {
		if ( $inline )
			$this->bIframe = true;
		else 
			$this->bIframe = false;
	}
	
	function pageparams ( ) {
		$Param = array( );
		if ( $this->bIframe ) {
			$Param['mode'] = 'inline';
		}
		else {
			$Param['mode'] = 'std';
		}
		return $Param;
	}
}
?>
